//
//  BuyQuanViewController.m
//  1243
//
//  Created by 杨森 on 16/4/7.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "BuyQuanViewController.h"
#import "BuyQuanTableViewCell.h"

#import "BuyQuanTwoTableViewCell.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "ConvertQuanViewController.h"
#import "SVProgressHUD.h"
@interface BuyQuanViewController () <UIAlertViewDelegate>

@end

@implementation BuyQuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];v
    
    self.title = @"购物购物券";
    self.tableView.rowHeight = 200;
    self.tableView.scrollEnabled = false;
    [self addUI];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)addUI
{
    CGFloat h = 30;
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT-64-h, WEITH, h)];
    [self.view addSubview:lastView];
//    lastView.backgroundColor = [UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1];
    lastView.backgroundColor = [UIColor whiteColor];
    self.view.backgroundColor = [UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1];
    
    UIButton *outDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lastView addSubview:outDateBtn];
    [outDateBtn setBackgroundImage:[UIImage imageNamed:@"查看过期券"] forState:UIControlStateNormal];
        CGFloat btnW = 100;
    outDateBtn.frame = CGRectMake(WEITH/2-btnW/2,5,btnW,15);
    
#warning 右上角按钮
//    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"兑换" style:UIBarButtonItemStylePlain target:self action:@selector(convertItemClick)];
//    self.navigationItem.rightBarButtonItem = item;
}

- (void)convertItemClick
{
    ConvertQuanViewController *conver = [[ConvertQuanViewController alloc]init];
    [self.navigationController pushViewController:conver animated:YES];
    
}


#pragma mark - Table view data source
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 3;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        static NSString *identifier= @"buyQuanCell";
        
        BuyQuanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"BuyQuanTableViewCell" owner:nil options:nil] lastObject];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        return cell;

    }else {
        
            static NSString *identifier= @"buyQuanTwoCell";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell==nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"BuyQuanTwoTableViewCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
        return cell;

    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200;
    }else{
        return 180;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        NSLog(@"购买购物券");
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确认购买购物券" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        alert.tag = 10086;
        
        [alert show];
        
//        [self buyGouWuQuan];
        
    }
}

- (void)buyGouWuQuan {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    NSString *Number = [dict objectForKey:@"Number"];
    
    NSDictionary *params = @{
                           @"Type":self.buyType,
                           @"UserNumber":Number,
                           @"ProductNumber":@"1",
                           //                           @"FromPassword":@"654321",
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"BUYCARD" completeBlock:^(id object) {
        
        NSLog(@"%@",object);
       
        if ([[object objectForKey:@"Id"] intValue] == 0) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"购买成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10010;
        
            [alert show];
        } else {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[object objectForKey:@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            alert.tag = 10011;
            
            [alert show];
        }
        
        
    }];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10010) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    if (alertView.tag == 10086) {
        if (buttonIndex == 1) {
            [SVProgressHUD show];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [SVProgressHUD dismiss];
                
                [self buyGouWuQuan];
                
            });
        }
    }
}

@end
